import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/login",
      name: "Login",
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(/* webpackChunkName: "about" */ "@/components/login.vue"),
    },
    {
      path: "/home",
      name: "home",
      component: () => import(/* webpackChunkName: "about" */ "@/components/home.vue"),
      redirect: '/welcome',
    children: [
     {path:"/welcome",
      name: "welcome",
      component: () => import(/* webpackChunkName: "about" */ "@/components/welcome.vue"),},
      {path:"/users",
      name: "users",
      component: () => import(/* webpackChunkName: "about" */ "@/components/user/Users.vue"),}  

    ] 
    },
    {
      path: "/",
      redirect: "/login",
    },
  ],
});
// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path=='/login') return next()
  // 获取token
  const tokenstr=window.sessionStorage.getItem('token')
  if(!tokenstr) return next('/login')
  next()
})

export default router;
